package com.desmart.renjiao.commission;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.desmart.renjiao.R;
import com.desmart.renjiao.bean.CommissionInfo;
import com.lemon.library.common.ARoutePath;
import com.lemon.library.common.CommonActivity;
import com.lemon.library.helper.RecycleViewDivider;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

@Route(path = ARoutePath.home.CommissionActivity)
public class CommissionActivity extends CommonActivity<CommissionM,CommissionP> implements Commission.CommissionV{

    private int pageNo = 1;
    private String field = "";
    private String order = "";
    private String type = "";
    private List<CommissionInfo> mList = new ArrayList<>();
    private RefreshLayout refreshLayout;
    private RecyclerView recyclerView;
    private CommissionAdapter adapter;
    private TextView tvSort;
    private TextView tvType;
    private String[] sortNames = new String[]{"按日期排序","按优先级排序","按发送人排序（A-Z）"};
    private String[] typeNames = new String[]{"全部","oa代办","sap代办"};
    private String[] orderNames = new String[]{"默认","正序","倒序"};
    private TextView tvOrder;

    @Override
    public int bindLayout() {
        return R.layout.activity_commission;
    }

    @Override
    public void initView(Activity activity) {
        setTitleText("代办");
        refreshLayout = activity.findViewById(R.id.refreshlayout);
        refreshLayout.setRefreshHeader(new ClassicsHeader(this));
        refreshLayout.setRefreshFooter(new ClassicsFooter(this));
        recyclerView = activity.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        tvSort = activity.findViewById(R.id.tv_sort);
        tvType = activity.findViewById(R.id.tv_type);
        tvOrder = activity.findViewById(R.id.tv_order);
        tvOrder.setOnClickListener(this);
        tvSort.setOnClickListener(this);
        tvType.setOnClickListener(this);
        adapter = new CommissionAdapter(mList);
        recyclerView.setAdapter(adapter);

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNo = 1;
                getCommision();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNo ++;
                getCommision();
            }
        });

        recyclerView.addItemDecoration(new RecycleViewDivider(
                this, LinearLayoutManager.VERTICAL, 2, getResources().getColor(R.color.F2F2F2)));

    }

    @Override
    public void initData(Context context) {
        showLoading();
        getCommision();
    }

    private void getCommision() {
        mPresenter.getCommissionList(pageNo,field,order,type);
    }

    @Override
    public void onCommissionListResult(List<CommissionInfo> data) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
        if (pageNo == 1)
            mList.clear();
        mList.addAll(data);
        adapter.notifyDataSetChanged();
        refreshLayout.setEnableAutoLoadMore(data.size() == 20);
    }

    @Override
    public void onNetFinish(String tag, String startMsg) {
        super.onNetFinish(tag, startMsg);
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == tvSort){
            sortshow();
        }else if (v == tvType){
            typeshow();
        }else if (v == tvOrder){
            ordershow();
        }


    }

    private void ordershow() {
        AlertDialog.Builder listDialog = new AlertDialog.Builder(this);
        listDialog.setItems(orderNames, (dialog, which) -> {
            tvOrder.setText(orderNames[which]);
            if (which == 0){
                order = "";
            }else if (which == 1){
                order = "asc";
            }else if (which == 2){
                order = "desc";
            }
            reInitData();
        });
        listDialog.show();
    }

    private void typeshow() {
        AlertDialog.Builder listDialog = new AlertDialog.Builder(this);
        listDialog.setItems(typeNames, (dialog, which) -> {
            tvType.setText(typeNames[which]);
            if (which == 0){
                type = "";
            }else if (which == 1){
                type = "oa";
            }else if (which == 2){
                type = "sap";
            }
            reInitData();
        });
        listDialog.show();
    }

    private void sortshow() {
        AlertDialog.Builder listDialog = new AlertDialog.Builder(this);
        listDialog.setItems(sortNames, (dialog, which) -> {
            tvSort.setText(sortNames[which]);
            if (which == 0){
                field = "sendDate";
            }else if (which == 1){
                field = "priority";
            }else if (which == 2){
                field = "taskFrom";
            }
            reInitData();
        });
        listDialog.show();
    }

    private void reInitData() {
        mList.clear();
        adapter.notifyDataSetChanged();
        showLoading();
        pageNo = 1;
        getCommision();
    }


}
